3D globe

#load("~/2019RPB/olist.RData")
#install all packages
#install.packages(c("ggplot2","ggmap","maptools","sp","maps","pacman","dplyr","tm","SnowballC","wordcloud","RColorBrewer","plotly","Imap","ggrepel"))

#load all packages
pacman::p_load(ggplot2,ggmap,maptools,sp,maps,dplyr,tm,SnowballC,wordcloud,RColorBrewer,plotly,Imap,ggrepel,tidyverse,geosphere,threejs,knitr,data.table,DT,RgoogleMaps,rworldmap,rgeos,raster,leaflet)

#m=complete2[c("selllng","selllat","custlng","custlat")]
#write.csv(m,file="~/2019RPB/brazilian-ecommerce/location.csv")
location=data.table::fread("~/2019RPB/brazilian-ecommerce/location.csv")
location=location[,-1]

# list.of.packages <-
#   c("geosphere", # For spatial methods
#     "threejs",   # threejs is used for 3-D interactive Earth Visualization
#     "rworldmap", # For creating earth map
#     "leaflet",   # Leaflet for R provides functions to control and integrate Leaflet, a JavaScript library for interactive maps, within R.
#     "rgeos",      # Provides functions for handling operations on topologies.
#     "raster",     # For raster image
#     "DT",         # For creating interactive tables
#     "ggplot2",
#     "sp"   ,       # For Spatial processing of data
#     "ggmap",       # To reverse geocode Long/Lat
#     "knitr"        # TO enable 3-D visualization embedding in the HTML page
#     )
#new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
#if(length(new.packages)) suppressMessages(suppressWarnings(install.packages(new.packages)))
#lapply(list.of.packages,function(x){suppressMessages(suppressWarnings(library(x,character.only=TRUE)))})
# Set this property to enable 3-D visualization
knit_hooks$set(webgl = location)

# Converting Data to spatial objects
seller_df<-data.frame(SellerLong=location$selllng,SellerLat=location$selllat)
# Create object of class SpatialPoints using SpatialPoints function from sp package
seller_sp<-SpatialPoints(seller_df, proj4string=CRS("+proj=longlat"))
#str(seller_sp)
#head(seller_sp)
# Convert to Spatial Dataframe
seller_spdf <- SpatialPointsDataFrame(seller_sp, data = seller_df)
#str(seller_spdf)
#head(seller_spdf)

# Similarly for the customers Locations
# Converting Data to spatial objects
cust_df<-data.frame(CustLong=location$custlng,CustLat=location$custlat)
# Create object of class SpatialPoints using SpatialPoints function from sp package
cust_sp<-SpatialPoints(cust_df, proj4string=CRS("+proj=longlat"))
# Convert to Spatial Dataframe
cust_spdf <- SpatialPointsDataFrame(cust_sp, data = cust_df)

# Create a Combined data frame for display purpose only
comb_df<-data.frame(location)
# Calculate distance between sellers and customers
comb_df$distance<-distHaversine(seller_sp,cust_sp)
comb_df$bearing<-bearing(cust_sp,seller_sp)
# Display the combined dataframe using interactive table 
datatable(comb_df, rownames = FALSE)
## Warning in instance$preRenderHook(instance): It seems your data is too
## big for client-side DataTables. You may consider server-side processing:
## https://rstudio.github.io/DT/server.html
# Plot seller points
plot(comb_df[3:4],col= "grey", pch = 3,xlab="long",ylab="lat",main="sellers vs customers")
# Add customer points
points(comb_df[1:2],col="blue")

#ggsave("sellers_cust.png", plot = last_plot())

Brazil = map_data("world") %>% filter(region=="Brazil")
brazilmap <- ggplot() + 
  geom_polygon(data = Brazil, aes(x=long, y = lat, group = group), fill="gray") + #巴西地圖
  geom_point(aes(x=comb_df$custlng, y=comb_df$custlat),color="blue", size=0.001) +  # Plot cust Location
  geom_point(aes(x=comb_df$selllng, y=comb_df$selllat),color="green", size=0.001) +  # Plot seller Locations
  scale_y_continuous(breaks = (-2:2) * 30) +
  scale_x_continuous(breaks = (-4:4) * 45) +
  theme_bw() +
  theme(axis.line = element_line(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank())
brazilmap

#world map
worldMap <- getMap()  # rworldmap function to get Long/Lat Polygon data for all countries 
world.points <- fortify(worldMap)  # Convert data into dataframe using fortify from ggplot2
## Regions defined for each Polygons
#head(world.points)
world.points$region <- world.points$id
#
world.df <- world.points[,c("long","lat","group", "region")]
#
worldmap <- ggplot() + 
  geom_polygon(data = world.df, aes(x = long, y = lat, group = group)) +
  geom_point(aes(x=comb_df$custlng, y=comb_df$custlat),color="blue", size=0.001) +  # Plot cust Location
  geom_point(aes(x=comb_df$selllng, y=comb_df$selllat),color="green", size=0.001) +  # Plot seller Locations
  scale_y_continuous(breaks = (-2:2) * 30) +
  scale_x_continuous(breaks = (-4:4) * 45) +
  theme_bw() +
  theme(axis.line = element_line(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank())
worldmap

#3D
worldmap +coord_map("ortho", orientation=c(5, -60, -10))+theme(panel.background = element_rect(fill = 'gray', colour = 'white'), panel.grid.major = element_line(color = "white"),
    panel.grid.minor = element_line(color = "white"))

worldmap <- ggplot() + 
  geom_polygon(data = world.df[world.df$region != 'Antarctica',], aes(x = long, y = lat, group = group)) +
  geom_point(aes(x=comb_df[,3], y=comb_df[,4]),color="blue", size=0.001) +  # Plot cust Location
  geom_point(aes(x=comb_df[,1], y=comb_df[,2]),color="green", size=0.001) +  # Plot seller Locations
  scale_y_continuous(breaks = (-2:2) * 30) +
  scale_x_continuous(breaks = (-4:4) * 45) +
  coord_map("azequidist",orientation=c(90, 0, 0))+
  theme(panel.background = element_rect(fill = 'azure3'))
worldmap

# Let us also plot top 50 such Source locations from where the flights crahsed frequently
# Approximate locations as factors rounding off Long / Lat to 2 decimal places
seller_da <- factor(sprintf("%.2f:%.2f",comb_df[,2], comb_df[,1]))
#str(seller_da)
#head(seller_da)
# A table of seller frequencies
freq <- sort(table(seller_da), decreasing=TRUE)
# The most frequent source airports in these data, possibly airports like NYC, London, Paris ?
frequent_destinations <- names(freq)[1:500]
# Subset the flight data by source frequency
idx <- seller_da %in% frequent_destinations
# Get the Long Lat data for source airports having most crash frequency
LongLat <- unique(comb_df[idx,1:2])
frequent_flights <-comb_df[idx,]

#wait tooooooooo long
earth <- system.file("images/world.jpg",  package="threejs") # Get the image of globe
# 3-D visual Dataframe
test_df <- data.frame(origin_lat = frequent_flights[,2], origin_long = frequent_flights[,1], dest_lat = frequent_flights[,4], dest_long = frequent_flights[,3])

globejs(img=earth, lat=LongLat[,2], long=LongLat[,1], arcs=test_df,
        arcsHeight=0.8, arcsLwd=1, arcsColor="green", arcsOpacity=0.3,
        atmosphere=TRUE, height=600, width = 600,bg = "white")